package com.yst.shader.feature.white_board

import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.core.view.children
import androidx.fragment.app.Fragment
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.interfaces.DraweeController
import com.facebook.imagepipeline.common.ResizeOptions
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.yst.shader.R
import com.yst.shader.ext.FocusListener
import com.yst.shader.ext.HoverListener
import com.yst.shader.feature.ss.effect.SSImageView
import com.yst.shader.feature.ss.effect.SSUtils
import kotlinx.android.synthetic.main.fragment_simple_board.view.*

/**
 * @author: xuyujian@ysten.com
 *  @date: 2020/8/28 15:20
 */
class SimpleBoardFragment : Fragment(R.layout.fragment_simple_board) {
    val images = listOf(
        "https://wx2.sinaimg.cn/mw690/5ba8d1cbly3ghv1yo5ippj20u00u0tds.jpg",
        "https://wx4.sinaimg.cn/mw690/5ba8d1cbly3ghv1ynzcaoj20go0gon4y.jpg",
        "https://wx4.sinaimg.cn/mw690/acff3e61ly1ghvvq3lfrmj21o02804qr.jpg",
        "https://wx4.sinaimg.cn/mw690/5ba8d1cbly3ghv1yni2tgj20u00u0h0c.jpg",
        "https://wx4.sinaimg.cn/mw690/acff3e61ly1ghvvq3lfrmj21o02804qr.jpg",
        "https://wx2.sinaimg.cn/mw690/daade500ly1ghv6p13yegj20zk0qo7wh.jpg",
//            "android.resource://${requireContext().packageName}/raw/a.gif",
//            "android.resource://${requireContext().packageName}/raw/b.gif",

//            "https://p2.ssl.qhimgs1.com/sdr/400__/t0146a8774d8a3933d8.gif"
//            "http://p0.ssl.qhimgs1.com/sdr/400__/t010e7771b60b3ad896.gif",
        "https://wx2.sinaimg.cn/mw690/daade500ly1ghv6p13yegj20zk0qo7wh.jpg"
    )

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.ll_parent.children
            .forEachIndexed { index, v ->
                (v as WhiteBoardImageView).isInnerBoard = index %2 == 0
                v.isFocusable = true
                v.isFocusableInTouchMode = true
                v.setOnHoverListener(HoverListener)

                if (v is SSImageView) {
                    v.onFocusChangeListener = FocusListener.apply {
                        callBack = { v, b ->
                            SSUtils.focus(v as SSImageView, b)
                           // v.bringToFront()
                        }
                    }
                    images.getOrNull(index)?.let {
                        v.controller = newController(it)
                    }
                }
            }
    }

    private fun newController(url: String): DraweeController {
        return Fresco.newDraweeControllerBuilder()
            .setUri(url)
            .setAutoPlayAnimations(true)
            .setImageRequest(let {
                ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setResizeOptions(ResizeOptions(200, 200))
                    .build()
            })
            .build();
    }

}